Skip to main content

Create Post resource CRUD via Laravel

php artisan make:model -mrc Post
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('title');
$table->string('content');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};

php artisan migrate
Route::resource('posts', PostController::class)
->only(['index', 'store', 'edit', 'update', 'destroy'])
->middleware(['auth', 'verified']);
<?php

namespace App\Http\Controllers;

use App\Models\Post;
use App\Models\User;
use Illuminate\Http\Request;

class PostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return view('posts.index', [
'posts' => Post::with('user')->latest()->get(),
]);
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:25',
'content' => 'required|string|max:255',
]);

$request->user()->posts()->create($validated);

return redirect(route('posts.index'));
}

/**
* Display the specified resource.
*/
public function show(Post $post)
{
//
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Post $post)
{
$this->authorize('update', $post);

return view('posts.edit', [
'post' => $post,
]);
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Post $post)
{
$this->authorize('update', $post);

$validated = $request->validate([
'title' => 'required|string|max:25',
'content' => 'required|string|max:255',
]);

$post->update($validated);

return redirect(route('post.index'));
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Post $post)
{
$this->authorize('delete', $post);

$chirp->delete();

return redirect(route('posts.index'));
}
}

php artisan make:policy PostPolicy --model=Post
<?php

namespace App\Policies;

use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\Response;

class PostPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Post $post): bool
{
//
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
//
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Post $post): bool
{
return $post->user()->is($user);
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Post $post): bool
{
return $this->update($user, $post);
}

/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Post $post): bool
{
//
}

/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Post $post): bool
{
//
}
}